﻿using KTD.Web.BLL;
using KTD.Web.Models;
using KTD.Web.Models.VD;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using XXS.Common;
using XXS.Common.ControllerExt;


namespace KTD.Web.Controllers
{
    public class HouseController : Controller
    {
        List<SelectListItem> desList = new List<SelectListItem>() {
            new SelectListItem(){Text="楼盘头条",Value="楼盘头条"},
            new SelectListItem(){Text="吃瓜吐槽",Value="吃瓜吐槽"},
            new SelectListItem(){Text="销售技巧",Value="销售技巧"},
            new SelectListItem(){Text="政策解读",Value="政策解读"}
        };
        List<SelectListItem> statusList = new List<SelectListItem>() {
             new SelectListItem(){Text="全部状态",Value=""},
            new SelectListItem(){Text="审核中",Value="0"},
            new SelectListItem(){Text="未通过",Value="1"},
            new SelectListItem(){Text="审核通过",Value="2"}
        };
        List<SelectListItem> orderList = new List<SelectListItem>() {
             new SelectListItem(){Text="按创建时间",Value="按创建时间"},
            new SelectListItem(){Text="按热度",Value="按热度"}
        };
        List<SelectListItem> saleTypeList = new List<SelectListItem>() {
            new SelectListItem(){Text="租/售",Value=""},
             new SelectListItem(){Text="租",Value="1"},
            new SelectListItem(){Text="售",Value="2"}
        };
        List<SelectListItem> saleTypesList = new List<SelectListItem>() {
            
             new SelectListItem(){Text="租",Value="1"},
            new SelectListItem(){Text="售",Value="2"}
        };
        // GET: House
        public ActionResult Index(HouseQD im)
        {

            if (string.IsNullOrEmpty(im.Order))
            {
                im.Order = "按创建时间";
            }
            var NewTypes = MF.DicManager.GetListBy(new DicQD() { PageSize = 40, DicType = DicType.新盘业态 }).PageData.Select(n=>new SelectListItem { Text=n.Value,Value=n.Value}).ToList();
            var SaleTypes = MF.DicManager.GetListBy(new DicQD() { PageSize = 40, DicType = DicType.租售业态 }).PageData.Select(n => new SelectListItem { Text = n.Value, Value = n.Value }).ToList();

            NewTypes.Insert(0, new SelectListItem { Text = "全部类型", Value = "" });
            SaleTypes.Insert(0, new SelectListItem { Text = "全部类型", Value = "" });
            im.IsLast = true;
            ViewBag.Channel = im.Channel;
            var res = MF.HouseManager.GetListBy(im);
            res.ControllerContext = ControllerContext;
            ViewBag.statusList = statusList;
            ViewBag.orderList = orderList;
            ViewBag.saleTypeList = saleTypeList;
            ViewBag.newTypes = NewTypes;
            ViewBag.saleTypes = SaleTypes;
            return View(res);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(HouseCUD im)
        {
          
            
            MF.HouseManager.Create(im);
            return RedirectToAction("Index", new { Channel = im.channel, Order= "按创建时间" });
        }

        [HttpGet]
        public ActionResult Create(string Channel)
        {
            var areaList = MF.AreaManager.GetListBy(new AreaQD() { PageSize = 100 }).PageData.OrderByDescending(n=>n.sort).ToList();
            var CityList = MF.AreaManager.GetListBy(new AreaQD() { PageSize = 100,isSon="false" }).PageData.ToList();

            ViewBag.saleTypesList = saleTypesList;
            Dictionary<int, SelectListGroup> keyValue = new Dictionary<int, SelectListGroup>();
            foreach (var item in CityList)
            {
                keyValue.Add(item.Id, new SelectListGroup() { Name = item.name });
            }

            var houseareaid = areaList.Select(n => new SelectListItem { Value = n.name.ToString(), Text = n.name, Group = keyValue[n.uniacid] }).ToList();
            houseareaid.Insert(0, new SelectListItem() { Text = "", Group = new SelectListGroup { Name = "四川省", Disabled=true } });
            ViewBag.houseareaid = houseareaid;
            var model = new HouseCUD();
            model.channel = Channel;
            return View(model);
        }



        [HttpGet]
        public ActionResult Update(int Id)
        {
            ViewBag.saleTypesList = saleTypesList;
            var areaList = MF.AreaManager.GetListBy(new AreaQD() { PageSize = 100 }).PageData.OrderByDescending(n => n.sort).ToList();
            var CityList = MF.AreaManager.GetListBy(new AreaQD() { PageSize = 100, isSon = "false" }).PageData.ToList();
            Dictionary<int, SelectListGroup> keyValue = new Dictionary<int, SelectListGroup>();
            foreach (var item in CityList)
            {
                keyValue.Add(item.Id, new SelectListGroup() { Name = item.name });
            }

            var houseareaid = areaList.Select(n => new SelectListItem { Value = n.name.ToString(), Text = n.name, Group = keyValue[n.uniacid] }).ToList();
            houseareaid.Insert(0, new SelectListItem() { Text = "", Group = new SelectListGroup { Name = "四川省", Disabled = true } });
            ViewBag.houseareaid = houseareaid;
            var model = MF.HouseManager.Find(Id);
            return View(model);
        }
        [HttpPost]

        [ValidateInput(false)]
        public ActionResult Update(HouseCUD im)
        {

            MF.HouseManager.Update(im);
            return RedirectToAction("Index", new { Channel = im.channel });
        }

        public ActionResult Delete(int Id)
        {
            try
            {
                MF.HouseManager.Delete(Id);
                return this.JsonNet(new { success = true }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        //[OutputCache(Duration = 60 * 60, VaryByParam = "*")]

        public ActionResult FindDetail(int Id, int? MemberId)
        {
            try
            {
                var res = MF.HouseManager.FindDetail(Id,MemberId);
                return this.JsonNet(new { success = true, data = res }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }


        public ActionResult Find(int Id)
        {
            try
            {
                var res = MF.HouseManager.Find(Id);
                res.content = HttpUtility.HtmlDecode(res.content);
                return this.JsonNet(new { success = true, data = res }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }


        [OutputCache(Duration = 60 * 60, VaryByParam = "*")]
        [HttpGet]
        public ActionResult GetList(HouseQD im)
        {
            try
            {
                if (im.Status == null) {
                    im.Status = Models.Enum.Status.审核通过;
                }
                im.Name = "";
                var res = MF.HouseManager.GetListBy(im);
                return this.JsonNet(res.ToExtGrid(), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpGet]
        public ActionResult GetLikeHouses(HouseQD im)
        {
            try
            {
                var res = MF.HouseManager.GetListBy(im);
                return this.JsonNet(res.ToExtGrid(), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message });
            }
        }
        [HttpPost]
        
        [ValidateInput(false)]
        public ActionResult WxCreateTemp()
        {

            try
            {
                MF.HouseManager.Create(new HouseCUD());
                return this.JsonNet(new { success = false } );
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        [ValidateInput(false)]
        public ActionResult WxCreate(HouseCUD im)
        {

            try
            {
                MF.HouseManager.WxCreate(im);
                return this.JsonNet(new { success = true });
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        [OutputCache(Duration = 60 * 60, VaryByParam = "*")]
        public ActionResult GetHosueMapList(HouseMapQD im)
        {
            //try
            //{
                var res = MF.HouseManager.GetHosueMapList(im);
                return this.JsonNet(res.ToExtGrid(), JsonRequestBehavior.AllowGet);
            //}
            //catch (Exception ex)
            //{
            //    return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            //}
        }
        public ActionResult FindNameListBy(HouseQD im)
        {
            
            var res = MF.HouseManager.FindNameListBy(im);
            return this.JsonNet(res.ToExtGrid(), JsonRequestBehavior.AllowGet);
           
        }
        

        [ValidateInput(false)]
        public ActionResult WxUpdate(HouseCUD im)
        {

            try
            {
                MF.HouseManager.Update(im);
                return this.JsonNet(new { success = false });
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }
    }
}